##################################################################
# /* Author: Gautam Nair and Nicholas Sambanis */
# /* Violence Exposure and Ethnic Identification: Evidence from Kashmir */
# /* Table 6 and associated Figure: Decadal Growth Rates in India and Casualties in India*/

##################################################################

# change working directory below and remove #
# setwd("")

##################################################################
# loading packages
##################################################################
library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
#library(plyr)
library(gridExtra)
library(dplyr)
library(scales)
library(grid)

##################################################################

rm(list=ls())

library(stargazer)

data.working <- read.csv("india_growth_insurgency_raw_data.csv")
head(data.working)
factorlevels <- c("1961-1969", "1970-1979", "1980-1989", "1990-1999", "2000-2009", "2010-2016")
data.working$decade_factor<- factor(data.working$decade, levels=factorlevels, ordered=TRUE)
data.working$total <- data.working$civilians + data.working$security_forces + data.working$terrorists

  D <- data.working$decade_factor
  Y <- data.working$gdp_growth_rate
  X <- data.working$total
  C <- data.working$civilians
  S <- data.working$security_forces
  I <- data.working$terrorists
  
  outcome <- cbind(D,Y, X, C, S, I)
  head(outcome)
  outcome <- as.data.frame(outcome)
  outcome <- group_by(outcome, D)
  outcome <- summarize(outcome, 
                       count=n(),
                       y_bar=mean(Y, na.rm=TRUE),
                       y_bar_100=format(round((mean(Y, na.rm=TRUE)), digits=1),nsmall=1),
                       x_bar=mean(X, na.rm=TRUE),
                       x_bar_100=format(round((mean(X, na.rm=TRUE)), digits=0),nsmall=0), 
                       x_sum=sum(X, na.rm=TRUE),
                       c_sum=sum(C, na.rm=TRUE),
                       s_sum=sum(S, na.rm=TRUE),
                       i_sum=sum(I, na.rm=TRUE)
                       )
outcome$x_sum_log<- outcome$x_sum
outcome$x_sum_log
outcome$x_sum_log[outcome$x_sum_log==0] <-1
outcome$x_sum_log <- log(outcome$x_sum_log)
outcome$x_sum_log_100 <- format(round(outcome$x_sum_log, digits=1),nsmall=)
outcome$x_sum_log_100

working.years <- factorlevels

working.table <- data.frame(cbind(working.years, outcome$y_bar_100,outcome$c_sum, outcome$s_sum, outcome$i_sum, outcome$x_sum, outcome$x_bar_100))

temp.labels <- c("Year", "Annual GDP Growth (Mean)", "Civilian Casualties (Sum)", "Security Forces Casualties (Sum)", "Insurgent Casualties (Sum)", "Total Casualties (Sum)", "Annual Casualties (Mean)")

colnames(working.table) <- temp.labels
working.table

table.title <- c("Table 6: GDP Growth and Insurgency-related Casualties in Kashmir by Decade")
output.file <- c("tf_t_06_growth_insurgency_casualties")
temptype <- c("latex", "text")
tempext <- c(".tex", ".txt")

for(q in 1:length(temptype)){
	temp.output <- paste(output.file, tempext[q], sep="")
	stargazer(working.table,
          out=temp.output,
         title= table.title,
          font.size="small",
		summary=FALSE,
		type= temptype[q]
	) 
}


temp.plot.1 <- ggplot(outcome, aes(x=D, y=y_bar, label=y_bar_100)) + geom_text(size=3.5, hjust=-0.1, vjust=2.0) +
           # geom_pointrange(aes(ymin=lower95.y, ymax=upper95.y), size=0.4, color="navyblue") +
           geom_line(color="navyblue", size=1.0) +
            theme_bw(base_size = 11) + theme(plot.title = element_text(size = 10), panel.grid.major= element_blank(),
            panel.grid.minor= element_blank(), axis.text=element_text(size=10), axis.title=element_text(size=10)) +
       scale_x_continuous(name="", breaks=c(1,2,3,4,5,6), labels=factorlevels, limits=c(1,6.2)) +
       scale_y_continuous(name="GDP Growth % (Decadal Average)", breaks=c(2,4,6,8), limits=c(2,8)) +
       theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))  
       temp.plot.1 <- temp.plot.1 + ggtitle("India Annual GDP Growth (Decadal Average)")
temp.plot.1

temp.plot.2 <- ggplot(outcome, aes(x=D, y=x_sum_log, label=x_sum_log_100)) + 
geom_text(data=subset(outcome, x_sum_log>1), size=3.5, hjust=+0.6, vjust=-1.0) +
           # geom_pointrange(aes(ymin=lower95.x, ymax=upper95.x), size=0.4, color="navyblue") +
            geom_line(color="red", size=1.0) +
            theme_bw(base_size = 11) + theme(plot.title = element_text(size = 10), panel.grid.major= element_blank(),
            panel.grid.minor= element_blank(), axis.text=element_text(size=10), axis.title=element_text(size=10)) +
       scale_x_continuous(name="", breaks=c(1,2,3,4,5,6), labels=factorlevels, limits=c(1,6.2)) +
              scale_y_continuous(name="Log(Total Casualties)", breaks=c(0,2,4,6,8,10), limits=c(0,12)) +
       theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm")) + 
       ylab("Annual Casualties (Log Decadal Sum)")
temp.plot.2 <- temp.plot.2 + ggtitle("Kashmir Total Casualties by Decade (Civilian, Insurgents, and Security Forces)")
temp.plot.2

png("tf_f_02_growth_insurgency_casualties.png", height = 1200, width = 900, units="px")
grid.newpage() 
grid.draw(rbind(ggplotGrob(temp.plot.1), ggplotGrob(temp.plot.2), size = "last"))
dev.off()
